<?php
namespace org\phpstruct\core;

/*
 *  Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Autoload
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.com>
 */
class Autoload {

  public static function init() {
	\spl_autoload_extensions('.php');
	\spl_autoload_register(__NAMESPACE__ . '\Autoload::load',true);
  }

  public static function load($class) {
	$ds = \DIRECTORY_SEPARATOR;
	$core = \realpath(\dirname(__FILE__).sprintf('%s..%s..%s..%s',$ds,$ds,$ds,$ds));
	$app = \realpath(sprintf('%s%s.phpstruct%slib%s',$_SERVER['DOCUMENT_ROOT'],$ds,$ds,$ds));
	$_core = $core . $ds . \str_replace('\\', $ds, $class . '.php');
	$_app = $app . $ds . \str_replace('\\', $ds, $class . '.php');
	if(\file_exists($_core)){
	  require_once($_core);
	} else {
//            throw new \Exception(Error::E_LOADER_FILENOTFOUND(), Error::E_LOADER_FILENOTFOUND, null);
        }
	if(\file_exists($_app)){
	  require_once($_app);
	} else {
//            throw new \Exception(Error::E_LOADER_FILENOTFOUND(), Error::E_LOADER_FILENOTFOUND, null);
        }
  }

}